package com.itheima.job;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        IDataOperationImpl iDataOperation = new IDataOperationImpl();
        Student stu1 = new Student("111","222","333","444");
        iDataOperation.add(stu1);
        System.out.println(iDataOperation);
        iDataOperation.delete("111");



    }
}

class IDataOperationImpl implements IDataOperation<Student>{
    private ArrayList<Student> stuList = new ArrayList<>();

    @Override
    public boolean add(Student t) {
        Iterator<Student> addIt = stuList.iterator();
        while (addIt.hasNext()){
            if (addIt.next().getId().equals(t.getId())){
                System.out.println("重复");
                return false;
            }
        }
        System.out.println("成功");
        stuList.add(t);
        return true;
    }

    @Override
    public <T> boolean delete(T t) {
        Iterator<Student> delIt = stuList.iterator();
        while (delIt.hasNext()){
            if (delIt.next().getId().equals(t)){
                delIt.remove();
                System.out.println("删除成功");
                return true;
            }
        }
        System.out.println("删除失败");
        return false;
    }

    @Override
    public <T> boolean update(T t) {
        for (int i = 0; i < stuList.size(); i++) {
            if (stuList.get(i).getId().equals(t)){
                Student stu = new Student();
                stu.setId(t+"");
                Scanner sc = new Scanner(System.in);
                stu.setName(sc.nextLine());
                stu.setAge(sc.nextLine());
                stu.setAddress(sc.nextLine());
                stuList.set(i,stu);
                System.out.println("修改成功");
                return true;
            }
        }
        System.out.println("修改失败");
        return false;
    }

    @Override
    public <T> Student get(T t) {
        return null;
    }

    @Override
    public Collection<Student> getAll() {
        return stuList;
    }
}

////数据操作接口
//interface IDataOperation {
//    // 添加数据
//    public boolean add(Student t);
//    // 根据唯一标识删除一个数据
//    public  boolean delete(Student t);
//    // 修改一个数据
//    public  boolean update(Student e);
//    // 根据唯一表示查找一个数据
//    public Student get(String t);
//    // 查找所有数据
//    public Collection<Student> getAll();
//}

//数据操作接口
interface IDataOperation<E> {
    // 添加数据
    public boolean add(E t);
    // 根据唯一标识删除一个数据
    public <T> boolean delete(T t);
    // 修改一个数据
    public <T> boolean update(T e);
    // 根据唯一表示查找一个数据
    public <T> E get(T t);
    // 查找所有数据
    public Collection<E> getAll();
}

// 标准学生类
class Student {
    private String id;// 学号
    private String name;// 姓名
    private String age;// 年龄
    private String address;// 居住地
    public Student() {}
    public Student(String id, String name, String age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
    }
}
